package com.android.djlmapp.tools.global;

import android.app.Activity;
import android.net.Uri;
import android.os.Environment;
import android.text.format.DateFormat;
import android.util.Log;

import com.jph.takephoto.app.TakePhoto;
import com.jph.takephoto.compress.CompressConfig;
import com.jph.takephoto.model.LubanOptions;
import com.jph.takephoto.model.TakePhotoOptions;

import java.io.File;
import java.util.Calendar;
import java.util.Locale;

/**
 * 第三方拍照配置
 * 2018/1/15
 * 张鹏
 */

public class TakePhotoUtils {
    public static final String STR_PATH = "/feihongtemp/";

    /**
     * @param takePhoto
     * @return
     */
    public static Uri configCamer(TakePhoto takePhoto) {

        File file = new File(getFileDirectory(), getSelfPhotoPath());
        Log.e("TakePhotoUtils", file.getParentFile().mkdirs() + "------configCamer------" + file.getPath());
        if (!file.getParentFile().exists()) {
            file.getParentFile().mkdirs();
        }
        Uri imageUri = Uri.fromFile(file);
        // 配置
        CompressConfig config;
        LubanOptions option = new LubanOptions.Builder().create();
        config = CompressConfig.ofLuban(option);
        config.enableReserveRaw(false); // 是否保留原图
        takePhoto.onEnableCompress(config, false); // 是否显示压缩进度条

        TakePhotoOptions.Builder builder = new TakePhotoOptions.Builder();
        builder.setCorrectImage(true);//纠正照片旋转角度
        takePhoto.setTakePhotoOptions(builder.create());
        return imageUri;
    }

    /**
     * 获取根目录
     *
     * @return
     */
    public static File getFileDirectory() {
        String sdDir = null;
        boolean sdCardExist = Environment.getExternalStorageState()
                .equals(android.os.Environment.MEDIA_MOUNTED);//判断sd卡是否存在
        Log.e("TakePhotoUtils", "------getFileDirectory------" + sdCardExist);
        if (sdCardExist) {
            sdDir = Environment.getExternalStorageDirectory().toString();//获取跟目录
            Log.e("TakePhotoUtils", "------sdCardExist------" + sdDir);
            return Environment.getExternalStorageDirectory();
        } else {
            sdDir = Environment.getRootDirectory().getAbsolutePath();
            Log.e("TakePhotoUtils", "------!sdCardExist!------" + sdDir);
            return Environment.getRootDirectory();
        }


    }

    /**
     * 获取自定义路径名称
     *
     * @return
     */
    public static String getSelfPhotoPath() {
        return STR_PATH + System.currentTimeMillis() + ".jpg";
    }

    /**
     * 获取时间格式化路径名称
     *
     * @return
     */
    public static String getDateFormatPhotoPath() {
        return STR_PATH + DateFormat.format("yyyyMMdd_hhmmss", Calendar.getInstance(Locale.CHINA)) + ".jpg";
    }

    /**
     * 获取文件名
     *
     * @return
     */
    public static String getDateFormatPhotoName() {
        return DateFormat.format("yyyyMMdd_hhmmss", Calendar.getInstance(Locale.CHINA)) + ".jpg";
    }

    /**
     * 获取保存路径
     *
     * @return
     */
    public static String getSavePath() {
        return getFileDirectory() + getSelfPhotoPath();
    }

    /**
     * 删除该路径下的所有图片
     */
    public static void deleteAllPhoto(Activity activity) {
        final String path = getFileDirectory() + STR_PATH;
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    File dir = new File(path);
                    if (dir == null || !dir.exists() || !dir.isDirectory()) {
                        return;
                    }
                    File[] files = dir.listFiles();
                    if (files.length > 0) {
                        for (File file : files) {
                            if (file.isFile()) {
                                Log.e("项目异常", "------删除文件------");
                                file.delete();
                            }
                        }
                    } else {
                        Log.e("项目异常", "------文件数组长度为0------");
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
}
